<?php
namespace App\Http\Controllers\ApiXcx;

use App\Http\Controllers\ApiXcx\BaseController;
use App\Service\Admin\ImageService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Filesystem\Filesystem;

class ImageController extends BaseController
{
    //文件

    //列表
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new ImageService())->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //保存
    public function upload(Request $request)
    {
        $img = $request->post('img', '');
        if (!$img) {
            return $this->toJson(-2, '图片必须的');
        }
        $fpath = Upload::getPathByBase64($img);
        //ddd($img, $fpath);
        //$fpath = env('DOMAIN') . '/' . $fpath;
        $sizes = getimagesize($img);
        $res = (new ImageService())->addOne($fpath, $sizes[0], $sizes[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $arys = [
            'domain' => env('DOMAIN') . '/',
            'img' => $fpath,
            'w' => $sizes[0],
            'h' => $sizes[1],
        ];
        return $this->toJson(0, '', $arys);
    }

    //保存
    public function uploadMultipleImages(Request $request)
    {
        try {
            $this->validate($request, [
                'images.*' => 'required|image|mimes:jpeg,png,jpg,gif|max:10240',
            ], [
                'images.*.required' => '图片必须上传',
                'images.*.mimes' => '图片格式不正确',
                'images.*.max' => '最大上传尺寸10M'
                ]);
            $img = $request->file('images', '');
            if (!$img) {
                return $this->toJson(-2, '图片必须的');
            }

            $images = $request->file('images');
            $folderName = 'upload/'.date('Ymd', time()).'/';
            $arys = [];
            foreach ($images as $image) {
                $extension = $image->getClientOriginalExtension();
                $safeName = time() . uniqid() . '.' . $extension;
                $fpath = $folderName . $safeName;
                $image->move($folderName, $safeName);
                $sizes = getimagesize($fpath);
                $arys[] = [
                    'domain' => env('DOMAIN') . '/',
                    'img' => $fpath,
                    'w' => $sizes[0],
                    'h' => $sizes[1],
                ];
                (new ImageService())->addOne($fpath, $sizes[0], $sizes[1]);
            }
            return $this->toJson(0, '', $arys);
        }catch (\Exception $e){
            $messages = $e->validator->messages()->getMessages();
            $error_msg = '';
            foreach ($messages as $key => $message) {
                $num_arr = explode('.', $key);
                $error_msg .= '第 ' . ($num_arr[1] + 1) . ' 张图片提交有误：' . $message[1] . ';';
            }
            return $this->toJson(-2, $error_msg);
        }
    }
}